
Imports System

Namespace _2bMatesSite.Common
	''' <summary>
	''' Summary description for DateUtilities.
	''' </summary>
	Public Enum Quarter
		First = 1
		Second = 2
		Third = 3
		Fourth = 4
	End Enum

	Public Enum Month
		January = 1
		February = 2
		March = 3
		April = 4
		May = 5
		June = 6
		July = 7
		August = 8
		September = 9
		October = 10
		November = 11
		December = 12
	End Enum

	Public Class DateUtilities
		#region Quarters

		Public Shared Function GetStartOfQuarter(ByVal Year As Integer, ByVal Qtr As Quarter) As DateTime
			If Qtr = Quarter.First Then
				Return New DateTime(Year, 1, 1, 0, 0, 0, _
					0)
ElseIf Qtr = Quarter.Second Then
				Return New DateTime(Year, 4, 1, 0, 0, 0, _
					0)
ElseIf Qtr = Quarter.Third Then
				Return New DateTime(Year, 7, 1, 0, 0, 0, _
					0)
			Else
				Return New DateTime(Year, 10, 1, 0, 0, 0, _
					0)
				' 1st Quarter = January 1 to March 31
				' 2nd Quarter = April 1 to June 30
				' 3rd Quarter = July 1 to September 30
				' 4th Quarter = October 1 to December 31
			End If
		End Function

		Public Shared Function GetEndOfQuarter(ByVal Year As Integer, ByVal Qtr As Quarter) As DateTime
			If Qtr = Quarter.First Then
				Return New DateTime(Year, 3, DateTime.DaysInMonth(Year, 3), 23, 59, 59, _
					999)
ElseIf Qtr = Quarter.Second Then
				Return New DateTime(Year, 6, DateTime.DaysInMonth(Year, 6), 23, 59, 59, _
					999)
ElseIf Qtr = Quarter.Third Then
				Return New DateTime(Year, 9, DateTime.DaysInMonth(Year, 9), 23, 59, 59, _
					999)
			Else
				Return New DateTime(Year, 12, DateTime.DaysInMonth(Year, 12), 23, 59, 59, _
					999)
				' 1st Quarter = January 1 to March 31
				' 2nd Quarter = April 1 to June 30
				' 3rd Quarter = July 1 to September 30
				' 4th Quarter = October 1 to December 31
			End If
		End Function

		Public Shared Function GetQuarter(ByVal Month As Month) As Quarter
			If Month <= Month.March Then
				Return Quarter.First
ElseIf (Month >= Month.April) AndAlso (Month <= Month.June) Then
				Return Quarter.Second
ElseIf (Month >= Month.July) AndAlso (Month <= Month.September) Then
				Return Quarter.Third
			Else
				Return Quarter.Fourth
				' 1st Quarter = January 1 to March 31
				' 2nd Quarter = April 1 to June 30
				' 3rd Quarter = July 1 to September 30
				' 4th Quarter = October 1 to December 31
			End If
		End Function

		Public Shared Function GetEndOfLastQuarter() As DateTime
			If DirectCast(DateTime.Now.Month, Month) <= Month.March Then
				Return GetEndOfQuarter(DateTime.Now.Year - 1, Quarter.Fourth)
			Else
				Return GetEndOfQuarter(DateTime.Now.Year, GetQuarter(DirectCast(DateTime.Now.Month, Month)))
				'go to last quarter of previous year
				'return last quarter of current year
			End If
		End Function

		Public Shared Function GetStartOfLastQuarter() As DateTime
			If DirectCast(DateTime.Now.Month, Month) <= Month.March Then
				Return GetStartOfQuarter(DateTime.Now.Year - 1, Quarter.Fourth)
			Else
				Return GetStartOfQuarter(DateTime.Now.Year, GetQuarter(DirectCast(DateTime.Now.Month, Month)))
				'go to last quarter of previous year
				'return last quarter of current year
			End If
		End Function

		Public Shared Function GetStartOfCurrentQuarter() As DateTime
			Return GetStartOfQuarter(DateTime.Now.Year, GetQuarter(DirectCast(DateTime.Now.Month, Month)))
		End Function

		Public Shared Function GetEndOfCurrentQuarter() As DateTime
			Return GetEndOfQuarter(DateTime.Now.Year, GetQuarter(DirectCast(DateTime.Now.Month, Month)))
		End Function
#End Region

		#region Weeks
		Public Shared Function GetStartOfLastWeek() As DateTime
			Dim DaysToSubtract As Integer = DirectCast(DateTime.Now.DayOfWeek, Integer) + 7
			Dim dt As DateTime = DateTime.Now.Subtract(System.TimeSpan.FromDays(DaysToSubtract))
			Return New DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0, _
				0)
		End Function

		Public Shared Function GetEndOfLastWeek() As DateTime
			Dim dt As DateTime = GetStartOfLastWeek().AddDays(6)
			Return New DateTime(dt.Year, dt.Month, dt.Day, 23, 59, 59, _
				999)
		End Function

		Public Shared Function GetStartOfCurrentWeek() As DateTime
			Dim DaysToSubtract As Integer = DirectCast(DateTime.Now.DayOfWeek, Integer)
			Dim dt As DateTime = DateTime.Now.Subtract(System.TimeSpan.FromDays(DaysToSubtract))
			Return New DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0, _
				0)
		End Function

		Public Shared Function GetEndOfCurrentWeek() As DateTime
			Dim dt As DateTime = GetStartOfCurrentWeek().AddDays(6)
			Return New DateTime(dt.Year, dt.Month, dt.Day, 23, 59, 59, _
				999)
		End Function
#End Region

		#region Months

		Public Shared Function GetStartOfMonth(ByVal Month As Month, ByVal Year As Integer) As DateTime
			Return New DateTime(Year, DirectCast(Month, Integer), 1, 0, 0, 0, _
				0)
		End Function

		Public Shared Function GetEndOfMonth(ByVal Month As Month, ByVal Year As Integer) As DateTime
			Return New DateTime(Year, DirectCast(Month, Integer), DateTime.DaysInMonth(Year, DirectCast(Month, Integer)), 23, 59, 59, _
				999)
		End Function

		Public Shared Function GetStartOfLastMonth() As DateTime
			If DateTime.Now.Month = 1 Then
				Return GetStartOfMonth(Month.December, DateTime.Now.Year - 1)
			Else
				Return GetStartOfMonth(DirectCast((DateTime.Now.Month - 1), Month), DateTime.Now.Year)
			End If
		End Function

		Public Shared Function GetEndOfLastMonth() As DateTime
			If DateTime.Now.Month = 1 Then
				Return GetEndOfMonth(Month.December, DateTime.Now.Year - 1)
			Else
				Return GetEndOfMonth(DirectCast((DateTime.Now.Month - 1), Month), DateTime.Now.Year)
			End If
		End Function

		Public Shared Function GetStartOfCurrentMonth() As DateTime
			Return GetStartOfMonth(DirectCast(DateTime.Now.Month, Month), DateTime.Now.Year)
		End Function

		Public Shared Function GetEndOfCurrentMonth() As DateTime
			Return GetEndOfMonth(DirectCast(DateTime.Now.Month, Month), DateTime.Now.Year)
		End Function
#End Region

		#region Years
		Public Shared Function GetStartOfYear(ByVal Year As Integer) As DateTime
			Return New DateTime(Year, 1, 1, 0, 0, 0, _
				0)
		End Function

		Public Shared Function GetEndOfYear(ByVal Year As Integer) As DateTime
			Return New DateTime(Year, 12, DateTime.DaysInMonth(Year, 12), 23, 59, 59, _
				999)
		End Function

		Public Shared Function GetStartOfLastYear() As DateTime
			Return GetStartOfYear(DateTime.Now.Year - 1)
		End Function

		Public Shared Function GetEndOfLastYear() As DateTime
			Return GetEndOfYear(DateTime.Now.Year - 1)
		End Function

		Public Shared Function GetStartOfCurrentYear() As DateTime
			Return GetStartOfYear(DateTime.Now.Year)
		End Function

		Public Shared Function GetEndOfCurrentYear() As DateTime
			Return GetEndOfYear(DateTime.Now.Year)
		End Function
#End Region

		#region Days
		Public Shared Function GetStartOfDay(ByVal [date] As DateTime) As DateTime
			Return New DateTime([date].Year, [date].Month, [date].Day, 0, 0, 0, _
				0)
		End Function

		Public Shared Function GetEndOfDay(ByVal [date] As DateTime) As DateTime
			Return New DateTime([date].Year, [date].Month, [date].Day, 23, 59, 59, _
				999)
		End Function
#End Region
	End Class
End Namespace






